package com.wiretap.action.admin.login;

import java.io.PrintWriter;
import java.util.Date;

import com.wiretap.action.WiretapActionSupport;
import com.wiretap.po.Administrator;
import com.wiretap.service.AdminService;
import com.wiretap.util.CommenUtil;
import com.wiretap.util.Constants;
import com.wiretap.util.DateUtils;

/**
 * @author zzy
 * @catagory 后台管理员登陆
 */
public class LoginAction extends WiretapActionSupport {

	/**
	 * 序列号
	 */
	private static final long serialVersionUID = -1610546509688140094L;
	private static final String CONTENT_TYPE = "text/html; charset=utf-8";
	// 用户名
	private String username;
	// 密码
	private String password;
	// 验证码
	private String validateCode;
	private AdminService adminService;

	/**
	 * 管理员登陆
	 * 
	 * @return
	 * @throws Exception
	 */
	public void login() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if (session.get(Constants.VALIDATE_CODE) != null && (CommenUtil.getString(session.get(Constants.VALIDATE_CODE)).equalsIgnoreCase(validateCode))) {
			Administrator admin = adminService.login(username, password);
			if (admin != null) {
				session.put(Constants.SESSION_USER, admin);
				session.put(Constants.SESSION_ONLINE_IP, request.getRemoteAddr());
				session.put(Constants.SESSION_ONLINE_TIME, DateUtils.getDateString(new Date(), Constants.DATEFORMAT_YYYY_MM_DD_HH_MM_SS));
				out.print(SUCCESS);
			} else {
				out.print(INPUT+"-"+getText("username.password.error"));
			}
		} else {
			out.print(INPUT + "-" + getText("validateCode.error"));
		}
		out.flush();
		out.close();
	}
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public void shutdown() throws Exception{
		
	} 
	
	/**
	 * 注销
	 * @return
	 * @throws Exception
	 */
	public String logout() throws Exception {
		session.clear();
		return SUCCESS;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the validateCode
	 */
	public String getValidateCode() {
		return validateCode;
	}

	/**
	 * @param validateCode
	 *            the validateCode to set
	 */
	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}

	/**
	 * @return the adminService
	 */
	public AdminService getAdminService() {
		return adminService;
	}

	/**
	 * @param adminService
	 *            the adminService to set
	 */
	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

}
